2950 #include "types.h"
2951 #include "defs.h"
2952 #include "param.h"
2953 #include "mmu.h"
2954 #include "proc.h"
2955 #include "x86.h"
2956 #include "traps.h"
2957 #include "spinlock.h"
2958 
2959 // Interrupt descriptor table (shared by all CPUs).
2960 struct gatedesc idt[256];
2961 extern uint vectors[];  // in vectors.S: array of 256 entry pointers
2962 struct spinlock tickslock;
2963 uint ticks;
2964 int tmpQuanta=0;
2965 
2966 void
2967 tvinit(void)
2968 {
2969   int i;
2970 
2971   for(i = 0; i < 256; i++)
2972     SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0);
2973   SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER);
2974 
2975   initlock(&tickslock, "time");
2976 }
2977 
2978 void
2979 idtinit(void)
2980 {
2981   lidt(idt, sizeof(idt));
2982 }
2983 
2984 void
2985 trap(struct trapframe *tf)
2986 {
2987   if(tf->trapno == T_SYSCALL){
2988     if(proc->killed)
2989       exit();
2990     proc->tf = tf;
2991     syscall();
2992     if(proc->killed)
2993       exit();
2994     return;
2995   }
2996 
2997   switch(tf->trapno){
2998   case T_IRQ0 + IRQ_TIMER:
2999     if(cpu->id == 0){
3000       acquire(&tickslock);
3001       ticks++;
3002       wakeup(&ticks);
3003       release(&tickslock);
3004     }
3005     lapiceoi();
3006     break;
3007   case T_IRQ0 + IRQ_IDE:
3008     ideintr();
3009     lapiceoi();
3010     break;
3011   case T_IRQ0 + IRQ_KBD:
3012     kbdintr();
3013     lapiceoi();
3014     break;
3015   case T_IRQ0 + IRQ_COM1:
3016     uartintr();
3017     lapiceoi();
3018     break;
3019   case T_IRQ0 + 7:
3020   case T_IRQ0 + IRQ_SPURIOUS:
3021     cprintf("cpu%d: spurious interrupt at %x:%x\n",
3022             cpu->id, tf->cs, tf->eip);
3023     lapiceoi();
3024     break;
3025 
3026   default:
3027     if(proc == 0 || (tf->cs&3) == 0){
3028       // In kernel, it must be our mistake.
3029       cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n",
3030               tf->trapno, cpu->id, tf->eip, rcr2());
3031       panic("trap");
3032     }
3033     // In user space, assume process misbehaved.
3034     cprintf("pid %d %s: trap %d err %d on cpu %d "
3035             "eip 0x%x addr 0x%x--kill proc\n",
3036             proc->pid, proc->name, tf->trapno, tf->err, cpu->id, tf->eip,
3037             rcr2());
3038     proc->killed = 1;
3039   }
3040 
3041    // Force process exit if it has been killed and is in user space.
3042   // (If it is still executing in the kernel, let it keep running
3043   // until it gets to the regular system call return.)
3044   if(proc && proc->killed && (tf->cs&3) == DPL_USER){
3045 	proc->etime = sys_uptime();
3046 	exit();
3047   }
3048 
3049 
3050   // Force process to give up CPU on clock tick.
3051   // If interrupts were on while locks held, would need to check nlock.
3052   if(proc && proc->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER){
3053 #ifdef SCHEDFLAG
3054 // if (SCHEDFLAG==1){
3055 if (tmpQuanta != QUANTA){
3056 		tmpQuanta++;
3057 		cprintf("tik no %d", tmpQuanta);}
3058   	else {
3059 
3060 		yield();
3061 		goto schedflag_label;
3062 // 	}
3063 }
3064 
3065 	if (SCHEDFLAG==1)
3066 		cprintf("Hiiiiiiiiiiiiiiii\n");
3067 #endif
3068 
3069 	yield();
3070   }
3071 schedflag_label:
3072 
3073   // Check if the process has been killed since we yielded
3074   if(proc && proc->killed && (tf->cs&3) == DPL_USER)
3075     exit();
3076 }
3077 
3078 
3079 
3080 
3081 
3082 
3083 
3084 
3085 
3086 
3087 
3088 
3089 
3090 
3091 
3092 
3093 
3094 
3095 
3096 
3097 
3098 
3099 
